package com.fast.develop.dm.integration;

import com.fast.develop.dm.core.MetaDomainConfiguration;
import lombok.SneakyThrows;

import javax.sql.DataSource;
import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-06-08 18:00
 * @desc：
 */
public class HibernateModuleConfigurationBuilder {

    private MetaDomainConfiguration metaDomainConfiguration;

    private DataSource dataSource;

    private String[] packagesToScan;

    private Class[] annotatedClasses;

    private String[] mappingResources;

    private Map properties;

    private boolean enableResourcesRest;


    public HibernateModuleConfigurationBuilder(MetaDomainConfiguration metaDomainConfiguration) {
        this.metaDomainConfiguration = metaDomainConfiguration;
    }

    public HibernateModuleConfigurationBuilder() {
        this.metaDomainConfiguration = new MetaDomainConfiguration();
    }


    @SneakyThrows
    public HibernateModuleConfiguration build(){
        metaDomainConfiguration.build();
        HibernateModuleConfiguration hibernateModuleConfiguration = new HibernateModuleConfiguration(metaDomainConfiguration);

        hibernateModuleConfiguration.setEnableResourcesRest(enableResourcesRest);
        hibernateModuleConfiguration.setDataSource(dataSource);
        hibernateModuleConfiguration.setMappingResources(mappingResources);
        hibernateModuleConfiguration.setAnnotatedClasses(annotatedClasses);
        hibernateModuleConfiguration.setPackagesToScan(packagesToScan);
        hibernateModuleConfiguration.setProperties(properties);

        hibernateModuleConfiguration.initialize();

        return hibernateModuleConfiguration;
    }

    public HibernateModuleConfigurationBuilder metaDomainConfiguration(MetaDomainConfiguration metaDomainConfiguration) {
        this.metaDomainConfiguration = metaDomainConfiguration;
        return this;
    }

    public HibernateModuleConfigurationBuilder dataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    public HibernateModuleConfigurationBuilder packagesToScan(String[] packagesToScan) {
        this.packagesToScan = packagesToScan;
        return this;
    }

    public HibernateModuleConfigurationBuilder enableResourcesRest() {
        this.enableResourcesRest = true;
        return this;
    }

    public HibernateModuleConfigurationBuilder annotatedClasses(Class[] annotatedClasses) {
        this.annotatedClasses = annotatedClasses;
        return this;
    }

    public HibernateModuleConfigurationBuilder mappingResources(String[] mappingResources) {
        this.mappingResources = mappingResources;
        return this;
    }

    public HibernateModuleConfigurationBuilder properties(Map properties) {
        this.properties = properties;
        return this;
    }
}
